<template>
     <Form class="xtx-form" ref="formCom"  v-slot="{errors}" :validation-schema="veeValidateSchema">
    <div class="user-info">
      <img :src="avatar" alt="" />
      <p>Hi，{{nickname}} 欢迎来小兔鲜，完成绑定后可以QQ账号一键登录哦~</p>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-phone"></i>
        <Field v-model="form.mobile" :class="{error:errors.mobile}" name='mobile' class="input" type="text" placeholder="绑定的手机号" />
      </div>
       <div v-if="errors.mobile" class="error">{{errors.mobile}}</div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-code"></i>
        <Field v-model="form.code" :class="{error:errors.code}" name='code' class="input" type="text" placeholder="短信验证码" />
          <span class="code" @click="send">{{number===0?'发送验证码':`${number}秒后发送`}}</span>
      </div>
      <div v-if="errors.code" class="error">{{errors.code}}</div>
    </div>
    <a href="javascript:;" class="submit" @click="loginBtn">立即绑定</a>
  </Form>
</template>

<script>
import { reactive, ref, watch } from 'vue'
import { Form, Field } from 'vee-validate'
import veeValidateSchema from '@/utils/vee-validate-schema'
import { userQQBindCode, userQQBindLogin } from '@/api/user'
import { userTimer } from '@/hooks'
import Message from '@/components/library/Message'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
export default {
  name: 'CallbackBind',
  components: {
    Form,
    Field
  },
  props: {
    unionId: {
      type: String,
      default: ''
    }
  },
  setup (props) {
    const store = useStore()
    const router = useRouter()
    const nickname = ref('')
    const avatar = ref('')
    // 公开信息
    if (QC.Login.check()) {
      QC.api('get_user_info').success(({ data }) => {
        // console.log(data)
        nickname.value = data.nickname
        avatar.value = data.figureurl_2
      })
    }
    const form = reactive({
      unionId: '',
      mobile: '',
      code: ''
    })
    // 监听unionId变化
    watch(() => props.unionId, (unionId) => {
      console.log(unionId)
      form.unionId = unionId
    }, { immediate: true })
    // 定时器
    const { number, formCom, send } = userTimer(() => userQQBindCode(form.mobile), form)

    // 点击绑定
    const loginBtn = async () => {
      const vaild = await formCom.value.validate()
      if (!vaild) return Message({ type: 'error', message: '表单校验失败' })
      try {
        const { result } = await userQQBindLogin(form)
        console.log(result)
        // 保存到本地
        store.commit('user/setUser', result)
        const redirectUrl = router.currentRoute.value.query.redirectUrl
        router.push(redirectUrl || '/')
        Message({ type: 'success', message: '绑定成功' })
      } catch (error) {
        Message({ type: 'error', message: error.response.data.message })
      }
    }

    return { nickname, avatar, form, veeValidateSchema, number, formCom, send, loginBtn }
  }
}
</script>

<style scoped lang='less'>
.user-info {
    width: 320px;
    height: 70px;
    margin: 0 auto;
    display: flex;
    background: #f2f2f2;
    align-items: center;
    padding: 0 10px;
    margin-bottom: 25px;
    img {
      background: #f2f2f2;
      width: 50px;
      height: 50px;
    }
    p {
      padding-left: 10px;
    }
  }
  .code {
    position: absolute;
    right: 0;
    top: 0;
    line-height: 50px;
    width: 80px;
    color: #999;
    &:hover {
      cursor: pointer;
    }
  }
</style>
